//
//  RLLiveController.m
//  RelaxLinkProduct
//
//  Created by relax on 2017/11/20.
//  Copyright © 2017年 relax. All rights reserved.
//

#import "RLLiveController.h"
#import "RLMiddleTitleView.h" // 中部视图
#import "RLAttentionController.h" // 关注
#import "RLHotController.h" // 热门
#import "RLNearByController.h" // 附近

@interface RLLiveController () <UIScrollViewDelegate>

@end

@implementation RLLiveController {
    RLAttentionController *_attentionVC;
    RLHotController *_hotVC;
    RLNearByController *_nearbyVC;
    RLMiddleTitleView *_titleView;
    UIScrollView *_vcScrollView;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // self.view.backgroundColor = [UIColor colorWithRed:0.28235 green:0.84314 blue:0.76863 alpha:1.00000];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.28235 green:0.84314 blue:0.76863 alpha:1.00000];
    // self.navigationController.navigationBar.translucent = NO;
    
    // 准备按钮
    [self prepareBarButtons];
    // 准备中部视图
    [self prepareMiddleTitleView];
    
    // 准备中间的视图
    [self prepareScrollView];
}

#pragma mark - 准备导航栏按钮
- (void)prepareBarButtons {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"UMS_find"] style:UIBarButtonItemStylePlain target:self action:@selector(findAction)];
    // UMS_email_icon
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"UMS_email_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(findAction)];

}

#pragma mark - 准备头部中部视图
- (void)prepareMiddleTitleView {
    RLMiddleTitleView *titleView = [RLMiddleTitleView middleTitleView];
    self.navigationItem.titleView = titleView;
    _titleView = titleView;
    __weak typeof(self) weakSelf = self;
    _titleView.buttonClicked = ^(MiddleTitleViewButtonType type) {
        __strong typeof(weakSelf) sself = weakSelf;
        [UIView animateWithDuration:0.25 animations:^{
            // setContentoffset ????????
            sself->_vcScrollView.contentOffset = CGPointMake(SCREEN_WIDTH * type, 0);
        }];
    };
}

#pragma mark - 准备子控制器视图
- (void)prepareScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    [self.view addSubview:scrollView];
    _vcScrollView = scrollView;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    // contentSize 的大小。
    // scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, SCREEN_HEIGHT);
    
    /**  关注控制器 */
    _attentionVC = [RLAttentionController new];
    /** 热门控制器 */
    _hotVC = [RLHotController new];
    /** 附近控制器 */
    _nearbyVC = [RLNearByController new];
    
    [scrollView addSubview:_attentionVC.view];
    [scrollView addSubview:_hotVC.view];
    [scrollView addSubview:_nearbyVC.view];
    
    /** 例行公事 */
    [self addChildViewController:_attentionVC];
    [self addChildViewController:_hotVC];
    [self addChildViewController:_nearbyVC];
    [_attentionVC didMoveToParentViewController:self];
    [_hotVC didMoveToParentViewController:self];
    [_nearbyVC didMoveToParentViewController:self];
 
   // [scrollView.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    [scrollView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.width.height.equalTo(scrollView);
    }];
    
    [scrollView.subviews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    _vcScrollView.delegate = self;
    
    // [_vcScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
//    // 让 scrollView 默认滚动到 index = 1的位置。
//    dispatch_async(dispatch_get_main_queue(), ^{
//        scrollView.contentOffset = CGPointMake(SCREEN_WIDTH * 1, 0);
//    });
    

}



- (void)findAction {
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x / scrollView.contentSize.width;
    // NSLog(@"%f",x);
    [_titleView moveLineViewWithScale:x];
    
}

static BOOL nearbyAnimationed = NO;
// 手动拖拽的时候，可以执行。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (!nearbyAnimationed) {
        CGFloat index = scrollView.contentOffset.x / scrollView.bounds.size.width;
        if (index == 2) {
            [_nearbyVC animationCells];
            nearbyAnimationed = YES;
        }
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSLog(@"%@",@"aaaa");
}


@end
